package com.example.dbcms.controller.admin;


import com.example.dbcms.annotation.Log;
import com.example.dbcms.service.UserService;
import com.example.dbcms.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Controller
@Api("管理员首页路由")
@RequestMapping("/admin/sys")
public class IndexController {

    /**
     * 注入userService
     */
    @Autowired
    private UserService userService;



    /**
     * 首页展示
     *
     * @return
     */
    @GetMapping("/about")
    @ApiOperation("默认跳转到首页")
    @Log(type="默认跳转到首页")
    public String about() {
        return "admin/home";
    }

    /**
     * 跳转到个人信息页面
     */
    @GetMapping("/userInfo")
    @ApiOperation("跳转到个人信息页面")
    @Log(type="跳转到个人信息页面")
    public String toUserInfo(int id, ModelMap modelMap) {
        modelMap.put("user", userService.getUserById(id));
        return "/index/userInfo";
    }

    /**
     * 跳转到修改密码页面
     */
    @GetMapping("/editPwd")
    @ApiOperation("跳转到修改密码页面")
    @Log(type="跳转到修改密码页面")
    public String toEditPwd() {
        return "/index/editPwd";
    }

    /**
     * 修改用户密码
     * @param original
     * @param password
     * @param confirm
     * @return
     */
    @PostMapping("/editPwd")
    @ResponseBody
    @ApiOperation("修改用户密码")
    @Log(type="修改用户密码")
    public ResultVo editPwd(String original,String password,String confirm){
        return userService.editPassword(original,password,confirm);
    }

    /**
     * 修改用户头像
     */
    @PostMapping("/userPicture")
    @ResponseBody
    @ApiOperation("修改用户头像")
    @Log(type="修改用户头像")
    public ResultVo userPicture(@RequestParam("picture") MultipartFile file) {
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        return userService.updatePicture(file,userAccount);
    }

    /**
     * 跳转到定时任务视图
     * @return
     */
    @GetMapping("/refreshTask")
    @ApiOperation("跳转到定时任务视图")
    @Log(type="跳转到定时任务视图")
    public String refreshTask(){
        return "admin/timedTask/index";
    }
}
